<?php

namespace api\business\controller;

use cmf\controller\RestAdminBaseController;

class ListsController extends RestAdminBaseController
{
    function index()
    {
        $model[] = [
            'name' => '卖家',
            'icon' => cmf_get_image_url('api/business/icon/group'),
            'type' => cmf_auth_check($this->userId, 'business/licenseplate/index') ? 1 : 9,
            'add' => cmf_auth_check($this->userId, 'business/licenseplate/add') ? 1 : 9,
            'baseurl' => url('/api/business/license_plate', '', false)
        ];

        $model[] = [
            'name' => '买家',
            'icon' => cmf_get_image_url('api/business/icon/Group4'),
            'type' => cmf_auth_check($this->userId, 'business/buyer/index') ? 1 : 9,
            'add' => cmf_auth_check($this->userId, 'business/buyer/add') ? 1 : 9,
            'baseurl' => url('/api/business/buyer', '', false)
        ];

        $model[] = [
            'name' => '售电',
            'icon' => cmf_get_image_url('api/business/icon/Group5'),
            'type' => cmf_auth_check($this->userId, 'business/shoudian/index') ? 1 : 9,
            'add' => cmf_auth_check($this->userId, 'business/shoudian/add') ? 1 : 9,
            'baseurl' => url('/api/business/shoudian', '', false)
        ];

        $model[] = [
            'name' => '中介',
            'icon' => cmf_get_image_url('api/business/icon/Group3'),
            'type' => cmf_auth_check($this->userId, 'business/medium/index') ? 1 : 9,
            'add' => cmf_auth_check($this->userId, 'business/medium/add') ? 1 : 9,
            'baseurl' => url('/api/business/medium', '', false)
        ];

        $model[] = [
            'name' => '虚拟地址',
            'icon' => cmf_get_image_url('api/business/icon/Group7'),
            'type' => cmf_auth_check($this->userId, 'business/virtualaddress/index') ? 1 : 9,
            'add' => cmf_auth_check($this->userId, 'business/virtualaddress/add') ? 1 : 9,
            'baseurl' => url('/api/business/virtual_address', '', false)
        ];

        $this->success('success', [
            'data_count' => [
                'name' => '当月录入量',
                'value' => $this->get_count() . '条',
            ],
            'model_list' => $model,
            'page' => [
                [
                    'name' => '学习会',
                    'icon' => cmf_get_image_url('api/business/icon/xuexi.png'),
                    'url' => 'http://m.xkf2017.com/lists?cid=2'
                ], [
                    'name' => '集团风采',
                    'icon' => cmf_get_image_url('api/business/icon/fengcai.png'),
                    'url' => 'http://m.xkf2017.com/lists?cid=3'
                ],
            ]
        ]);
    }

    function get_count()
    {
        $starttime = strtotime(date('Y-m-01'));
        $result['license_plate_user'] = db('license_plate_user')->where([
            'level' => 1,
            'uid' => $this->userId,
            'create_time' => [
                'egt', $starttime
            ]
        ])->field('count("id") as size')->find();

        $result['buyer_user'] = db('buyer_user')->where([
            'level' => 1,
            'uid' => $this->userId,
            'create_time' => [
                'egt', $starttime
            ]
        ])->field('count("id") as size')->find();

        $result['medium'] = db('medium')->where([
            'uid' => $this->userId,
            'create_time' => [
                'egt', $starttime
            ]
        ])->field('count("id") as size')->find();

        $result['shoudian'] = db('shoudian')->where([
            'uid' => $this->userId,
            'create_time' => [
                'egt', $starttime
            ]
        ])->field('count("id") as size')->find();

        $result['virtual_address'] = db('virtual_address')->where([
            'uid' => $this->userId,
            'create_time' => [
                'egt', $starttime
            ]
        ])->field('count("id") as size')->find();

        $size = 0;
        foreach ($result as $val) {
            $size += $val['size'];
        }
        return $size;
    }
}